﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BUS.DAL;

namespace BUS
{
    public class ChiTietDatHangBUS
    {
        #region PROPERTIES
        public int ID { get; set; }

        public DatHangBUS DatHang { get; set; }

        #region SanPham
        public SanPhamBUS SanPham { get; set; }
        public int IDSanPham
        {
            get
            {
                return SanPham == null ? 0 : SanPham.ID;
            }
        }
        public string MaSanPham
        {
            get
            {
                return SanPham == null ? String.Empty : SanPham.MaSanPham;
            }
        }
        public string TenSanPham
        {
            get
            {
                return SanPham == null ? String.Empty : SanPham.TenSanPham;
            }
        }
        public int IDLoaiSanPham
        {
            get
            {
                return SanPham == null ? 0 : SanPham.LoaiSanPham.ID;
            }
        }
        public string TenLoaiSanPham
        {
            get
            {
                return SanPham == null ? String.Empty : SanPham.TenLoaiSanPham;
            }
        }
        public string Anh
        {
            get
            {
                return SanPham == null ? String.Empty : SanPham.Anh;
            }
        }
        public decimal Gia
        {
            get
            {
                return SanPham == null ? decimal.Zero : SanPham.Gia;
            }
        }
        public string strGia
        {
            get
            {
                return SanPham == null ? String.Empty : SanPham.strGia;
            }
        }
        public int SoTonSanPham
        {
            get
            {
                return SanPham == null ? 0 : SanPham.SoTon;
            }
        }
        #endregion

        private int soLuong;
        public int SoLuong
        {
            get
            {
                return soLuong == 0 ? 0 : soLuong;
            }
            set { soLuong = value; }
        }

        private decimal thanhTien;
        public decimal ThanhTien
        {
            get
            {
                return thanhTien;
            }
            set { thanhTien = value; }
        }

        public string strThanhTien
        {
            get
            {
                return String.Format("{0:0,0 VNĐ}", thanhTien);
            }
        }

        public bool Duyet { get; set; }


        #endregion

        public void Delete()
        {
            ChiTietDatHangDAL.Delete(this);
        }

        public void UpdateDuyet()
        {
            ChiTietDatHangDAL.UpdateDuyet(this);
        }

        public static List<ChiTietDatHangBUS> GetProductsByCatesAndDate(int idCate, DateTime from, DateTime to)
        {
            List<ChiTietDatHangBUS> list = ChiTietDatHangDAL.GetProductsByCatesAndDate(from, to);
            if (idCate > 0)
                list = list.Where(p => p.IDLoaiSanPham == idCate).ToList<ChiTietDatHangBUS>();
            return list.OrderByDescending(p => p.SoLuong).ToList<ChiTietDatHangBUS>();
        }
    }
}
